package com.bonc.cq.cloud.controller;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/instance")
public class InstanceController {
    private static final Logger logger = LoggerFactory.getLogger(InstanceController.class);

    @Value("${server.port}")
    private int port;

    @GetMapping(value = "/getInstance/{serviceId}")
    public JSONObject getInstance(@PathVariable String serviceId){
        logger.info("Get Instance by serviceId {} port {}", serviceId,port);
        JSONObject resJson = new JSONObject();
        resJson.put("serviceId",serviceId);
        resJson.put("detail","hello world");
        resJson.put("provider port",port);
        return resJson;
    }

    @RequestMapping(value = "/deleteInstanceByServiceId/{serviceId}", method = RequestMethod.DELETE)
    public String deleteInstanceByServiceId(@PathVariable("serviceId") String serviceId) {
        logger.info("Delete Instance by serviceId {} port {}", serviceId, port);
        return "Instance whose serviceId is " + serviceId + " is deleted";

    }

    @RequestMapping(value = "/createInstance", method = RequestMethod.POST)
    public String createInstance(@RequestBody JSONObject instance) {
        logger.info("Create Instance whose serviceId is {} port {}", instance.getString("serviceId"), port);
        return "Instance whose serviceId is" + instance.getString("serviceId") + " is created";
    }

    @RequestMapping(value = "/updateInstanceByServiceId/{serviceId}", method = RequestMethod.PUT)
    public String updateInstanceByServiceId(@RequestBody JSONObject instance, @PathVariable("serviceId") String serviceId) {
        logger.info("Update Instance whose serviceId {} port {}", serviceId, port);
        return "Instance whose serviceId is " + serviceId + " is updated";
    }
}
